package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentMyFollowPagerBinding;
import com.ja.openchat.ui.base.BaseFragmentPagerAdapter;
import com.ja.openchat.viewmodels.MyFollowPagerGroupViewModel;
import com.ja.openchat.views.CustomEditText;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

//我的关注和粉丝
public class MyFollowPagerGroupFragment extends BaseFragment<FragmentMyFollowPagerBinding, MyFollowPagerGroupViewModel> {

    private List<Fragment> mFragments;
    private List<String> titlePager;

    private int type;
    private long userId;

    @Override
    public MyFollowPagerGroupViewModel initViewModel() {
        return ViewModelProviders.of(getActivity()).get(MyFollowPagerGroupViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getInt("type");
            userId = arguments.getLong("userId");
        }
    }

    protected List<Fragment> pagerFragment() {
        List<Fragment> list = new ArrayList<>();
        list.add(MyFollowFragment.newInstance(0, userId));
        list.add(MyFollowFragment.newInstance(1, userId));
        return list;
    }

    protected List<String> pagerTitleString() {
        List<String> list = new ArrayList<>();
        list.add(getString(R.string.unfocus_text));
        list.add(getString(R.string.fan_text));
        return list;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my_follow_pager;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        mFragments = pagerFragment();
        titlePager = pagerTitleString();
        //设置Adapter
        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager);
        binding.viewPager.setAdapter(pagerAdapter);
        binding.tabs.setupWithViewPager(binding.viewPager);
        if (type == 1) {
            binding.viewPager.setCurrentItem(1);
            binding.titleBar.setTitle(getString(R.string.fan_text));
        } else {
            binding.titleBar.setTitle(getString(R.string.unfocus_text));
        }
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.titleBar.setTitle(position == 0 ? getString(R.string.unfocus_text) : getString(R.string.fan_text));
                binding.etSearch.setText("");
                MyFollowFragment fragment = (MyFollowFragment) mFragments.get(binding.viewPager.getCurrentItem());
                fragment.doSearch();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initView() {
        super.initView();
        binding.etSearch.setOnXTextChangeListener(new CustomEditText.OnXTextChangeListener() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                viewModel.search.set(s.toString());
                viewModel.searchAction.setValue(s.toString());
                MyFollowFragment fragment = (MyFollowFragment) mFragments.get(binding.viewPager.getCurrentItem());
                fragment.doSearch();
            }
        });
    }

    @Override
    public void initViewObservable() {

    }
}
